package com.health.trans.repository;

import com.alibaba.fastjson.JSONObject;
import com.health.entity.OpcPrescription;
import com.health.entity.OpcPrescriptionPK;
import com.health.trans.util.CustomRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

/**
 * Repository
 * @author qyh
*/
public interface OpcPrescriptionRepository extends CustomRepository<OpcPrescription, OpcPrescriptionPK>, JpaSpecificationExecutor<OpcPrescription> {
    /**
     * 获取处方明细关联值
     *
     * @param hospCode        hosp代码
     * @param prescriptMainId 规定主要id
     * @return {@link JSONObject}
     */
    @Query(nativeQuery = true, value = GET_CONVERT_JSON_SQL)
    Integer getConvertJson(@Param("hospCode") String hospCode,
                              @Param("prescriptMainId") String prescriptMainId);


    String GET_CONVERT_JSON_SQL ="select count(b.prescript_detail_id) as detailCount " +
            "from (select * from tb_opc_prescription where prescript_main_id=:prescriptMainId and hosp_code=:hospCode) a\n" +
            "left join tb_opc_prescription_detail b\n" +
            "on a.prescript_main_id=b.prescript_main_id and a.hosp_code=b.hosp_code\n";

}
